package com.yq.spbYq.controller;


import com.yq.spbYq.Points;
import com.yq.spbYq.service.impl.PointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/points")
public class PointsController {

    @Autowired
    private PointsService pointsService;

    // 获取所有积分记录
    @GetMapping("/getAll")
    public ResponseEntity<List<Points>> getAllPoints() {
        List<Points> pointsList = pointsService.getAllPoints();
        return ResponseEntity.ok(pointsList);
    }

    // 根据居民ID获取积分记录
    @GetMapping("/getByResidentId/{residentId}")
    public ResponseEntity<Points> getPointsByResidentId(@PathVariable Integer residentId) {
        Points points = pointsService.getPointsByResidentId(residentId);
        if (points != null) {
            return ResponseEntity.ok(points);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 添加积分记录
    @PostMapping("/add")
    public ResponseEntity<Points> addPoints(@RequestBody Points points) {
        int result = pointsService.addPoints(points);
        if (result > 0) {
            return ResponseEntity.ok(points);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 更新积分记录
    @PutMapping("/update")
    public ResponseEntity<Points> updatePoints(@RequestBody Points points) {
        int result = pointsService.updatePoints(points);
        if (result > 0) {
            return ResponseEntity.ok(points);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据居民ID删除积分记录
    @DeleteMapping("/delete/{residentId}")
    public ResponseEntity<Void> deletePointsByResidentId(@PathVariable Integer residentId) {
        int result = pointsService.deletePointsByResidentId(residentId);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}